<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
*/

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */

class calendarsViewcalendars extends JView
{
	
    function display($tpl = null)
    {
		//var_dump('here');
		global $option, $mainframe;
		
		if($this->getLayout() == 'form') {
			//var_dump('view, display');
			$this->_displayForm($tpl);
			return;
		}
        JToolBarHelper::title(   JText::_( 'Calendar Manager' ), 'generic.png' );
		JToolBarHelper::archiveList();
		JToolBarHelper::unarchiveList();
		JToolBarHelper::publishList();
		JToolBarHelper::unpublishList();
        JToolBarHelper::deleteList();
        JToolBarHelper::editListX();
        JToolBarHelper::addNewX();
		// add pagination
		//$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		//$limitstart = JRequest::getVar('limitstart', 0);
		
		/**
		* use new model method veiwCalendar()
		*/
		$model	=& $this->getModel();
		$data_object =& $model->viewCalendar();
		
		//var_dump('view');
        // Get data from the model
      	$items =  $this->get('Data');
		
		//var_dump($items);
		$this->assignRef('items', $items);
		$this->assignRef('rows', $data_object->rows);
		$this->assignRef('lists', $data_object->lists);
		$this->assignRef('pagination', $data_object->pagination);
		$this->assignRef('redirect', $data_object->redirect);
		///////////////////////////////////////////
		// Create the form
		//$form = new JParameter('', JPATH_COMPONENT.DS.'models'.DS.'event.xml');

		// Details Group
		//$active = (intval($item->created_by) ? intval($row->created_by) : $user->get('id'));
		//$form->set('created_by', $active);
		//$form->set('access', $row->access);
		//$form->set('created_by_alias', $row->created_by_alias);

		//$form->set('created', JHTML::_('date', $row->date, '%Y-%m-%d %H:%M:%S'));
		//$form->set('publish_up', JHTML::_('date', $row->publish_up, '%Y-%m-%d %H:%M:%S'));
		//$form->set('publish_down', $row->publish_down);

		// Advanced Group
		//$form->loadINI($row->attribs);

		// Metadata Group
		//$form->set('description', $row->description);
		//$form->set('keywords', $row->metakey);
		//$form->loadINI($row->metadata);
		///////////////////////////////////////////
		parent::setLayout('list');//tpl = 'list';
        parent::display($tpl);
    }
	

	function _displayForm($tpl)
	{
		global $mainframe, $option;

		//jimport('joomla.html.pane');
		
		$db		=& JFactory::getDBO();
		$uri 	=& JFactory::getURI();
		$user 	=& JFactory::getUser();
		$model	=& $this->getModel();
		//$pane	=& JPane::getInstance('sliders');
		
		//JHTML::_('behavior.tooltip');
		//JHTML::_('behavior.calendar');		
		
		$lists = array();

		//get the weblink
		//var_dump($model);
		$model->_setBuild('detail');
		//var_dump();
		
		//JToolBarHelper::title(   JText::_( 'Calendar Manager' ), 'generic.png' );
        //JToolBarHelper::deleteList();
        //JToolBarHelper::editListX();
        //JToolBarHelper::addNewX();
		$data =& $this->get('Data');
		$event = (is_array($data)) ? $data[0] : $data;
		/*
		 * We need to unify the introtext and fulltext fields and have the
		 * fields separated by the {readmore} tag, so lets do that now.
		 */
		if (JString::strlen($event->fulldescription) > 1) {
			$event->description = $event->introdescription . "<hr id=\"system-readmore\" />" . $event->fulldescription;
		} else {
			$event->description = $event->introdescription;
		}
		/**
		* patch to bridge use of param date and startdate column
		*/
		$event->reg = new JRegistry('com_calendar');
		$event->reg->loadini($event->params);
		
		
		$isNew		= ((int)$event->id < 1);
		//var_dump($event->id);
		// fail if checked out not by 'me'
		/*
		if ($model->isCheckedOut( $user->get('id') )) {
			$msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The weblink' ), $event->title );
			$mainframe->redirect( 'index.php?option='. $option, $msg );
		}
		*/
		
		// Set toolbar items for the page
		$text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
		JToolBarHelper::title(   JText::_( 'Calendar' ).': <small><small>[ ' . $text.' ]</small></small>' );
		JToolBarHelper::save();
		if ($isNew)  {
			JToolBarHelper::cancel();
		} else {
			// for existing items the button is renamed `close`
			JToolBarHelper::cancel( 'cancel', 'Close' );
		}
		JToolBarHelper::help( 'screen.calendar.edit' );

		// Edit or Create?
		//var_dump($isNew);
		
		if (!$isNew)
		{
			//$model->checkout( $user->get('id') );
		}
		else
		{
			// initialise new record
			//$event->published = 1;
			$event->state = 1;
			$event->approved 	= 1;
			$event->order 	= 0;
			$event->catid 	= JRequest::getVar( 'catid', 0, 'post', 'int' );
		}

		// build the html select list for ordering
		$query = 'SELECT ordering AS value, title AS text'
			. ' FROM #__calendar'
			//. ' WHERE catid = ' . (int) $calendar->catid
			. ' ORDER BY ordering';
		$lists['ordering'] 			= JHTML::_('list.specificordering',  $event, $event->id, $query, 1 );
		if(substr($lists['ordering'], 0, 57) == '<input type="hidden" name="ordering" value="" />New items')
		{
		$lists['ordering'] 			= "Items are ordered by date, but giving a default ordering number of '0'. Ordering can be changed after this item is saved.";
		}
		// build list of categories
		$lists['catid'] 			= JHTML::_('list.category',  'catid', $option, intval( $event->catid ) );
		// build the html select list
		//$lists['published'] 		= JHTML::_('select.booleanlist',  'published', 'class="inputbox"', $event->state );
		$lists['state'] 		= JHTML::_('select.booleanlist',  'state', 'class="inputbox"', $event->state );

		//clean weblink data
		//jimport('joomla.filter.output');
		//JOutputFilter::objectHTMLSafe( $event, ENT_QUOTES, 'description' );
		
		// make time selector
		$time_selector_obj = $model->time_selector();
		
		if(!empty($event->start_time)){
			$time = explode(':', $event->start_time); 
			$timestamp = mktime($time[0],$time[1],$time[2]);
			$twelvehr = date('g:i:s:A',$timestamp);
			if($twelvehr != false){
				$stored_time_array = explode(':', $twelvehr);
			}
		/*
			
		*/

		}//end if start_time
				//hour
		//var_dump($time_selector_obj);
		$event->time_selector = new stdclass();
		$event->time_selector->hour_select = '<select class="inputbox" name="start_time[';
		$event->time_selector->hour_select .= "hours";
		$event->time_selector->hour_select .= ']">';
		foreach($time_selector_obj->hours as $key=>$value){
			//var_dump('cycle '.$key);
			$event->time_selector->hour_select .= '<option value="';
			$event->time_selector->hour_select .= $key;
			$event->time_selector->hour_select .= '"';
			if(!empty($stored_time_array) && $stored_time_array[0] == $key){
				$event->time_selector->hour_select .= ' selected="selected" ';
			}elseif( empty($stored_time_array) && (int)$key == 7){
				$event->time_selector->hour_select .= ' selected="selected" ';
			}
			$event->time_selector->hour_select .= '>';
			$event->time_selector->hour_select .= $value;
			$event->time_selector->hour_select .= "</option>";
		}
		$event->time_selector->hour_select .= "</select>";
		//var_dump($event->time_selector);
		$event->time_selector->minute_select = '<select class="inputbox" name="start_time[';
		$event->time_selector->minute_select .= "minutes";
		$event->time_selector->minute_select .= ']">';
		foreach($time_selector_obj->minutes as $key=>$value){
			//var_dump('cycle '.$key);
			$event->time_selector->minute_select .= '<option value="';
			$event->time_selector->minute_select .= $key;
			if(!empty($stored_time_array) && $stored_time_array[1] == $key){
				$event->time_selector->minute_select .= ' selected="selected" ';
			}
			$event->time_selector->minute_select .= '">';
			$event->time_selector->minute_select .= $value;
			$event->time_selector->minute_select .= "</option>";
		}
		$event->time_selector->minute_select .= "</select>";
		//second
		$event->time_selector->second_select = '<select class="inputbox" name="start_time[' . "seconds" . ']">';
		foreach($time_selector_obj->seconds as $key=>$value){
			$event->time_selector->second_select .= '<option value="'.$key.'"';
			if(!empty($stored_time_array) && $stored_time_array[2] == $key){
				$event->time_selector->second_select .= ' selected="selected" ';
			}
			$event->time_selector->second_select .=	'">'.$value."</option>";
		}
		$event->time_selector->second_select .= '</select>';
		//am_pm
		$event->time_selector->am_pm_select = '<select class="inputbox" name="start_time[' . "am_pm" . ']">';
		foreach($time_selector_obj->am_pm as $key=>$value){
			$event->time_selector->am_pm_select .= '<option value="'.$key.'"';
			if(!empty($stored_time_array) && $stored_time_array[3] == $value){
				$event->time_selector->am_pm_select .= ' selected="selected" ';
			}elseif( empty($stored_time_array) && (int)$key == 1){
				$event->time_selector->am_pm_select .= ' selected="selected" ';
			}
			$event->time_selector->am_pm_select .= '>'.$value."</option>";
		}
		$event->time_selector->am_pm_select .= '</select>';
		//var_dump($event->time_selector);
		///////////
		$file 	= JPATH_ADMINISTRATOR .'/components/com_calendar/models/event.xml';
		$params = new JParameter( $event->params, $file, 'component' );

		$this->assignRef('lists',		$lists);
		$this->assignRef('event',		$event);
		$this->assignRef('params',		$params);
		$this->assignRef('time',		$time_selector_obj);
		$this->assignRef('request_url',	$uri->toString());


		parent::display($tpl);
	}

}

?>
